<?php
namespace App\Admin\Controllers\Operation;

use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;

use App\Models\Operation\WarehouseEntry as ThisModel;
use App\Models\Garage\Garage;
use App\Models\Garage\User;
use App\Models\Garage\Vehicle;

class WarehouseEntryController extends AdminController
{
    protected $title = '入库管理';

    protected function grid(): Grid
    {
        $types = ThisModel::getTypes();
        $garages = Garage::getGarages();
        $users = User::getUsers();

        $grid = new Grid(new ThisModel);

        $grid->model()->orderBy('id', 'desc');

        $grid->column('id', __('ID'))->sortable();
        $grid->column('vehicle_id', __('Vehicle'))->display(function ($vehicle_id) {
            return $vehicle_id ? sprintf("<a href='/admin/garage/vehicles?id=%d'>%s</a>", $vehicle_id, $this->vehicle->model) : __('Unknown');
        });
        $grid->column('garage_id', __('Garage'))->display(function ($garage_id) {
            return $garage_id ? sprintf("<a href='/admin/garage/garages?id=%d'>%s</a>", $garage_id, $this->garage->title) : __('Unknown');
        });
        $grid->column('entry_type', __('Entry type'))->using($types);
        $grid->column('entry_at', __('Entry at'));
        $grid->column('driver_name', __('Driver name'));
        $grid->column('driver_phone', __('Driver phone'));
        // $grid->column('address', __('Address'));
        // $grid->column('remark', __('Remark'));
        $grid->column('created_by', __('Created by'))->display(function ($val) use ($users) {
            return sprintf('<a href="/admin/garage/users?id=%s" target="_blank">%s</a>', $val, $users[$val] ?: __("Unknown"));
        });
        $grid->column('created_at', __('Created at'));
        $grid->column('updated_at', __('Updated at'));

        $grid->filter(function ($filter) use ($types, $garages) {
            $filter->disableIdFilter();
            $filter->column(1/3, function ($filter) use ($types) {
                $filter->equal('id', __('ID'));
                $filter->equal('type', __('Type'))->select($types);
                // $filter->equal('model', __('Model'));
            });
            $filter->column(1/3, function ($filter) use ($garages) {
                $filter->equal('garage_id', __('Garage'))->select($garages);
                $filter->equal('driver_name', __('Driver name'));
                $filter->equal('driver_phone', __('Driver phone'));
            });
            $filter->column(1/3, function ($filter) {
                $filter->between('created_at', __('Created at'))->date();
                $filter->between('updated_at', __('Updated at'))->date();
                $filter->between('entry_at', __('Entry at'))->date();
            });
        });

        $grid->disableCreateButton();
        $grid->disableBatchActions();
        $grid->actions(function (Grid\Displayers\Actions $actions) {
            $actions->disableDelete();
            $actions->disableView();
        });

        return $grid;
    }
    protected function detail($id): Show
    {
        $show = new Show(ThisModel::findOrFail($id));

        $show->field('id', __('ID'));
        // $show->field('created_at', __('Created at'));
        // $show->field('updated_at', __('Updated at'));

        return $show;
    }
    protected function form(): Form
    {
        $types = ThisModel::getTypes();
        $vehicle_types = Vehicle::getTypes();
        $presents = Vehicle::getPresents();
        $users = User::getUsers();

        $form = new Form(new ThisModel);
        // 不可修改
        // $form->display('id', __('ID'));
        $form->select('entry_type', __('Entry type'))->options($types)->readOnly();
        $form->display('entry_at', __('Entry at'));
        $form->display('driver_name', __('Driver name'));
        $form->display('driver_phone', __('Driver phone'));
        // 不可修改
        $form->select('vehicle.type', __('Vehicle type'))->options($vehicle_types)->readOnly();
        $form->display('vehicle.model', __('Vehicle model'));
        $form->display('vehicle.plate_number', __('Plate number'));
        $form->display('vehicle.chassis_number', __('Chassis number'));
        // 可修改
        $form->select('vehicle.key_present', __('Key present'))->options($presents);
        $form->select('vehicle.registration_present', __('Registration present'))->options($presents);
        $form->select('vehicle.door', __('Door'))->options($presents);
        $form->select('vehicle.items', __('Items'))->options($presents);
        $form->select('vehicle.damage', __('Damage'))->options($presents);
        $form->select('vehicle.seal', __('Seal'))->options($presents);
        // 可修改
        $form->display('created_by', __('Created by'))->with(function ($value) use ($users) {
            return $users[$value] ?? '未知';
        });
        $form->text('address', __('Address'));
        // $form->display('created_at', __('Created At'));
        // $form->display('updated_at', __('Updated At'));
        $form->model()->form($form);

        $form->disableViewCheck()
            ->disableCreatingCheck();

        $form->tools(function (Form\Tools $tools) {
            $tools->disableDelete();
            $tools->disableView();
        });

        return $form;
    }
}
